package com.siwei.mes.controller.system;

import com.siwei.mes.common.query.PageQuery;
import com.siwei.mes.common.result.ApiResult;
import com.siwei.mes.common.result.Json;
import com.siwei.mes.common.validator.AddGroup;
import com.siwei.mes.common.validator.PageGroup;
import com.siwei.mes.common.validator.UpdateGroup;
import com.siwei.mes.entity.CheckBatchConfig;
import com.siwei.mes.enums.MaterialsEnum;
import com.siwei.mes.service.CheckBatchConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 批检指标配置相关
 *
 * @author linzi
 * @since 2023-11-25 22:22:18
 */
@Api(value = "批检指标配置")
@RestController
@RequestMapping("checkBatchConfig")
public class CheckBatchConfigController {
    @Resource
    private CheckBatchConfigService checkBatchConfigService;

    @ApiOperation(value = "新增批检指标配置")
    @PostMapping("/create")
    public Json create(@RequestBody @Validated(AddGroup.class) CheckBatchConfig entity) {
        Integer projectCategory = entity.getProjectCategory();
        if (!projectCategory.equals(MaterialsEnum.CONCRETE.getCode())) {
            /**如果不是混凝土 需要判断物料信息*/
            assertTrue(StringUtils.isBlank(entity.getProjectName()), "产品名称为空");
            assertTrue(StringUtils.isBlank(entity.getProjectNameCode()), "产品名称code为空");
            assertTrue(StringUtils.isBlank(entity.getProjectSpecs()), "产品规格为空");
            assertTrue(StringUtils.isBlank(entity.getProjectSpecsCode()), "产品规格code为空");
        }
        /**如果批检规格等于  2-按重量*/
        if (entity.getBatchRules() == 2) {
            assertTrue(entity.getBatchThreshold() == null, "批检阈值为空");
        }
        return Json.succ().data(ApiResult.addColumn("id", checkBatchConfigService.create(entity)));
    }

    @ApiOperation(value = "修改批检指标配置")
    @PostMapping("/update")
    public Json update(@RequestBody @Validated(UpdateGroup.class) CheckBatchConfig entity) {
        Integer projectCategory = entity.getProjectCategory();
        if (!projectCategory.equals(MaterialsEnum.CONCRETE.getCode())) {
            /**如果不是混凝土 需要判断物料信息*/
            assertTrue(StringUtils.isBlank(entity.getProjectName()), "产品名称为空");
            assertTrue(StringUtils.isBlank(entity.getProjectNameCode()), "产品名称code为空");
            assertTrue(StringUtils.isBlank(entity.getProjectSpecs()), "产品规格为空");
            assertTrue(StringUtils.isBlank(entity.getProjectSpecsCode()), "产品规格code为空");
        }
        /**如果批检规格等于  2-按重量*/
        if (entity.getBatchRules() == 2) {
            assertTrue(entity.getBatchThreshold() == null, "批检阈值为空");
        }
        checkBatchConfigService.update(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "修改必检")
    @PostMapping("/updateMust")
    public Json updateMust(@RequestBody CheckBatchConfig entity) {
        assertTrue(entity.getId() == null, "批检指标主键id为空");
        assertTrue(entity.getIsMust() == null, "是否必检为空");
        checkBatchConfigService.updateByPrimaryKeySelective(entity);
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "删除批检指标配置")
    @PostMapping("/delete")
    public Json delete(@RequestBody CheckBatchConfig entity) {
        checkBatchConfigService.delete(entity.getId());
        return Json.succ().data(new ApiResult());
    }

    @ApiOperation(value = "分页获取批检指标配置")
    @PostMapping("/page")
    public Json appPage(@RequestBody @Validated(PageGroup.class) PageQuery<CheckBatchConfig, CheckBatchConfig> query) {
        return Json.succ().data(checkBatchConfigService.getPage(query));
    }

    @ApiOperation(value = "根据主键id获取批检指标配置")
    @GetMapping("loadById")
    public Json loadById(@RequestParam("id") Long id) {
        return Json.succ().data(checkBatchConfigService.loadById(id));
    }

    @ApiOperation(value = "获取所有批检指标配置")
    @PostMapping("getAll")
    public Json getAll(@RequestBody CheckBatchConfig entity) {
        return Json.succ().data(ApiResult.addList(checkBatchConfigService.getAll(entity)));
    }

}
